﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using Taobao.WangWang.Common;
using System.Configuration;

namespace Taobao.WangWang
{
    public partial class ChatLogForm : Form
    {
        #region Ctor

        public ChatLogForm()
        {
            InitializeComponent();
        }

        #endregion

        #region Fields

        private TextBoxWriter customtw;
        private Thread job;

        #endregion

        #region Page Load
        private DateTime minDatetime = DateTime.Today.AddDays(-1);
        private DateTime maxDatetime = DateTime.Today.AddDays(-1);
        private int fixDis = 7;

        private void Form1_Load(object sender, EventArgs e)
        {
            this.customtw = new TextBoxWriter(this.OutputTextBox);
            Console.SetOut(this.customtw);

            int minDate = -100;
            int.TryParse(ConfigurationManager.AppSettings["MinDate"] ?? "-100", out minDate);
            if (minDate > -1)
            {
                minDate = -1;
            }
            minDatetime = DateTime.Today.AddDays(minDate);

            int.TryParse(ConfigurationManager.AppSettings["FixDis"] ?? "7", out  fixDis);
            if (fixDis <= 0)
            {
                fixDis = 7;
            }  
            NoteLabel.Text = string.Format("开始时间先于结束时间，并且不能相差{0}天", fixDis);

            StartDateTimePicker.MinDate = minDatetime;
            StartDateTimePicker.MaxDate =maxDatetime;
            StartDateTimePicker.Value = maxDatetime;
            EndDateDateTimePicker.MinDate = minDatetime;
            EndDateDateTimePicker.MaxDate = maxDatetime;
            EndDateDateTimePicker.Value = maxDatetime;
             
        }

        #endregion

        #region Jobs

        private void DoJob()
        {
            try
            {
                using (var entities = Model.Util.LoadEntities())
                {
                    ClearOutput();
                    Console.WriteLine("程序开始");
                    var allShops = entities.Shops.Include("ShopManagers").ToList();
                    foreach (var shop in allShops)
                    {
                        try
                        {
                            Console.WriteLine(string.Format("开始下载店铺:{0}的聊天记录", shop.Name));
                            WangWangProcessor processor = new WangWangProcessor(shop);
                            processor.StartDate = StartDateTimePicker.Value;
                            processor.EndDate = EndDateDateTimePicker.Value;
                            processor.Process();
                            Console.WriteLine(string.Format("下载店铺:{0}的聊天记录结束", shop.Name));
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(string.Format("下载店铺:{0}的聊天记录异常", shop.Name));
                        }
                    }
                    Thread.Sleep(3000);
                    Console.WriteLine("程序完成");
                    StartButton.Text = "开始下载";
                }
            }
            catch (Exception e)
            { 
                this.job.Abort();
            }
        }

        private void ClearOutput()
        {
            OutputTextBox.Text = string.Empty;
        }

        private void Run()
        {
            this.job = new Thread(new ThreadStart(this.DoJob));
            this.job.IsBackground = true;
            this.job.Start();
        }

        #endregion

        #region button Events

        private void StartButton_Click(object sender, EventArgs e)
        {
            
            if(EndDateDateTimePicker.Value.Subtract(StartDateTimePicker.Value).Days>fixDis)
            {
                MessageBox.Show(string.Format("开始时间和结束时间最多只能差{0}天。",fixDis));
                StartDateTimePicker.Value = EndDateDateTimePicker.Value;
                return;
            }

            if (StartButton.Text == "开始下载")
            {
                Run();             
                StartButton.Text = "暂停下载";
            }
            else if (StartButton.Text == "暂停下载")
            {
                if (this.job != null)
                    this.job.Suspend();
                StartButton.Text = "继续下载";
            }
            else if (StartButton.Text == "继续下载")
            {
                if (this.job != null)
                    this.job.Resume();
                StartButton.Text = "暂停下载";
            }
        }

        #endregion

        private void StartDateTimePicker_ValueChanged(object sender, EventArgs e)
        {
            if (StartDateTimePicker.Value.CompareTo(EndDateDateTimePicker.Value) > 0)
            {
                MessageBox.Show("开始时间不能大于结束时间。");
                StartDateTimePicker.Value = EndDateDateTimePicker.Value;
            }
            //EndDateDateTimePicker.MinDate = StartDateTimePicker.Value;
            //EndDateDateTimePicker.MaxDate = StartDateTimePicker.Value.AddDays(fixDis).CompareTo(maxDatetime) > 0 ? maxDatetime : StartDateTimePicker.Value.AddDays(fixDis);

        }

        private void EndDateDateTimePicker_ValueChanged(object sender, EventArgs e)
        {
            if (StartDateTimePicker.Value.CompareTo(EndDateDateTimePicker.Value) > 0)
            {
                MessageBox.Show("结束时间不能小于开始时间。");
                EndDateDateTimePicker.Value = StartDateTimePicker.Value;
            }
            //else if (EndDateDateTimePicker.Value.Subtract(StartDateTimePicker.Value).Days > fixDis)
            //{
            //    MessageBox.Show(string.Format("开始时间和结束时间最多只能差{0}天。", fixDis));
            //    EndDateDateTimePicker.Value = StartDateTimePicker.Value;
            //}
            //StartDateTimePicker.MinDate = EndDateDateTimePicker.Value.AddDays(-fixDis).CompareTo(minDatetime) > 0 ? EndDateDateTimePicker.Value.AddDays(-fixDis) : minDatetime;
            //StartDateTimePicker.MaxDate = EndDateDateTimePicker.Value;
        }
    }
}
